home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Development Kits / HyperCard Related / APDA HyperCard Toolkits / HyperCard Serial Toolkit 2.6 / Source Code / recvChars.p < prev    next >
Encoding:
Text File  |  1995-02-07  |  2.4 KB  |  103 lines  |  [TEXT/MPS ]

  1. (*
  2.     recvChars(count) -- Return count characters from the serial port.
  3.  
  4.     To compile and link this file using Macintosh Programmer's Workshop,
  5.  
  6.         pascal -w recvChars.p
  7.         link -m ENTRYPOINT -o HyperCommands -rt XFCN=7031 -sn Main=recvChars ∂
  8.             recvChars.p.o "{MPW}"Libraries:interface.o "{MPW}"Libraries:Libraries:HyperXLib.o
  9.  
  10.     © Copyright 1987,88,89 by Apple Computer, Inc.
  11.  
  12.     Initial coding 9/87 by Harry R. Chesley.
  13. *)
  14.  
  15. {$R-}
  16.  
  17. {$S recvChars }     { Segment name must be the same as the command name. }
  18.  
  19. unit DummyUnit;
  20.  
  21. interface
  22.  
  23. uses MemTypes, QuickDraw, OSIntf, HyperXCmd;
  24.  
  25. procedure EntryPoint(paramPtr: XCmdPtr);
  26.     
  27. implementation
  28.  
  29. procedure recvChars(paramPtr: XCmdPtr); forward;
  30.  
  31. procedure EntryPoint(paramPtr: XCmdPtr);
  32.  
  33.     begin
  34.         recvChars(paramPtr);
  35.     end;
  36.  
  37. procedure recvChars(paramPtr: XCmdPtr);
  38.  
  39.     var readCount: longInt;
  40.         readCountStr: Str255;
  41.         l, l2: longInt;
  42.         p: Ptr;
  43.  
  44.     procedure Fail(errMsg: Str255); { set theResult and quit }
  45.         begin
  46.             paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
  47.             exit(recvChars);
  48.         end;
  49.  
  50.     {$I SPortUtil.inc}
  51.  
  52.     begin
  53.         if paramPtr^.paramCount <> 1 then Fail('parameter count is not 1');
  54.  
  55.         ZeroToPas(paramPtr,paramPtr^.params[1]^,readCountStr);        { First parameter is count to read. }
  56.         readCount := StrToNum(paramPtr,readCountStr);
  57.         if readCount < 0 then Fail('invalid count');
  58.  
  59.         SetUpSPortGlobals;
  60.         EnsureOpenPort;
  61.  
  62.         { Create the input handle. }
  63.         paramPtr^.returnValue := NewHandle(readCount+1);
  64.         HLock(paramPtr^.returnValue);
  65.         { Read, read, read... }
  66.         if readCount > 0 then
  67.             begin
  68.                 l := readCount;
  69.                 if FSRead(ThisSPort.portInDev,l,paramPtr^.returnValue^) <> noErr then
  70.                     begin
  71.                         DisposHandle(paramPtr^.returnValue);
  72.                         Fail('FSRead failed');
  73.                     end;
  74.                 { Shrink the handle if we didn't get everything (paranoid programing; this should be impossible). }
  75.                 if l <> readCount then SetHandleSize(paramPtr^.returnValue,l+1);
  76.             end
  77.         else l := 0;
  78.         { Tack on a zero termination byte. }
  79.         p := ptr(ord4(paramPtr^.returnValue^)+l);
  80.         p^ := 0;
  81.         { Should we strip? }
  82.         if ThisSPort.stripIncoming then
  83.             begin
  84.                 { If so, rip dem bits off. }
  85.                 p := paramPtr^.returnValue^;
  86.                 for l2 := 1 to l do
  87.                     begin
  88.                         p^ := BAND(p^,$7F);
  89.                         p := pointer(ord4(p)+1);
  90.                     end;
  91.             end;
  92.         { Eliminate any internal zeros. }
  93.         p := paramPtr^.returnValue^;
  94.         for l2 := 1 to l do
  95.             begin
  96.                 if p^ = 0 then p^ := ord('¿');
  97.                 p := pointer(ord4(p)+1);
  98.             end;
  99.         HUnlock(paramPtr^.returnValue);
  100.     end;
  101.  
  102. end.
  103.